package com.toefl.speaking.handler;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.message.BasicNameValuePair;

import com.toefl.speaking.util.AppConstans;
import com.toefl.speaking.util.AppUtils;

public class QuestionHandler {

	/** 题目相关 */
	private String QUESTION = "question/";
	/** 请求题目列表方法 */
	private String LIST = "list/";
	/** 请求题目内容方法 */
	private String DETAIL = "detail/";

	/** 收藏相关 */
	private String FAVORITES = "favorites/";
	/** 添加收藏 */
	private String ADD = "add/";
	/** 删除收藏 */
	private String DETELTE = "del/";
	/** 下载题目音频 */
	private String DOWNLOAD = "download/";
	/** 下载题目音频题目 */
	private String TOPIC = "topic/";
	/** 批改 */
	private String CORRECT = "correct/";

	private static QuestionHandler instance;

	private Question question;

	private QuestionHandler() {

	}

	public static QuestionHandler instance() {
		if (instance == null) {
			instance = new QuestionHandler();
		}
		return instance;
	}

	public Question getQuestionCorrect() {
		if (question == null) {
			question = new Question();
		}
		return question;
	}

	/**
	 * 请求题目列表
	 */
	public String postQuestionList(String questionType) throws ClientProtocolException, IOException {
		return AppUtils.doGet(AppConstans.INTERFACE_URL + QUESTION + LIST + questionType);
	}

	/**
	 * 题目内容
	 */
	public String postQuestionDetail(int questionId) throws ClientProtocolException, IOException {
		return AppUtils.doGet(AppConstans.INTERFACE_URL + QUESTION + DETAIL + questionId);
	}

	/**
	 * 设置收藏
	 */
	public String postAddFavorites(String questionId, String favoritesJson) throws ClientProtocolException, IOException {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair(AppConstans.InfoList, favoritesJson));
		return AppUtils.doPost(AppConstans.INTERFACE_URL + QUESTION + ADD + questionId, nameValuePairs);
	}

	/**
	 * 删除收藏
	 */
	public String postDeleteFavorites(String questionId, String favoritesJson) throws ClientProtocolException,
			IOException {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair(AppConstans.FidList, favoritesJson));
		return AppUtils.doPost(AppConstans.INTERFACE_URL + FAVORITES + DETELTE + questionId, nameValuePairs);
	}

	/**
	 * 下载题目音频
	 */
	public String postDownloadQuestionAudio(int questionId) throws ClientProtocolException, IOException {
		return AppUtils.doGet(AppConstans.INTERFACE_URL + DOWNLOAD + TOPIC + questionId, true);
	}

	/**
	 * 批改音频
	 * @param question
	 * @param context
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public String postQuestionCorrect(int questionId, File doPostWithFile) throws ClientProtocolException, IOException {
		return AppUtils.doPostWithFile(AppConstans.INTERFACE_URL + QUESTION + CORRECT + questionId, doPostWithFile);
	}

	//
	// //
	// // public String postQuestionCorrectSaved(Context context) throws ClientProtocolException, IOException {
	// // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	// // return AppUtils.httpPost(AppConstans.INTERFACE_URL, nameValuePairs);
	// // }
	// //
	// // public String postQuestionDel(Question question, Context context) throws ClientProtocolException, IOException
	// {
	// // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	// // // nameValuePairs.add(new BasicNameValuePair(AppConstans.QidList, question.getQuidList()));
	// // return AppUtils.httpPost(AppConstans.INTERFACE_URL, nameValuePairs);
	// // }
	// //
	// // public String postQuestionHistory(Context context) throws ClientProtocolException, IOException {
	// // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	// // return AppUtils.httpPost(AppConstans.INTERFACE_URL, nameValuePairs);
	// // }
	// //
	// // // public String postQuestionList(Context context) throws ClientProtocolException, IOException {
	// // // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	// // // return AppUtils.httpPost(AppConstans.INTERFACE_URL, nameValuePairs);
	// // // }
	// //
	// // public String postQuestionSave(Question question, Context context) throws ClientProtocolException, IOException
	// {
	// // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	// // // nameValuePairs.add(new BasicNameValuePair(AppConstans.DeviceToken, questionCorrect.getUserFile()));
	// // return AppUtils.httpPost(AppConstans.INTERFACE_URL, nameValuePairs);
	// // }

	public class Question {
		// private String
	}

}
